package com.ajieee.merge.runner.finance;

import com.ajieee.merge.entity.finance.po.StockInfoPO;
import com.ajieee.merge.enums.finance.EtlType;
import com.ajieee.merge.enums.finance.Exchange;
import com.ajieee.merge.mapper.StockInfoMapper;
import com.ajieee.merge.util.DateUtils;
import com.fasterxml.jackson.databind.JsonNode;
import lombok.RequiredArgsConstructor;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import org.springframework.web.client.RestClient;
import org.springframework.web.util.UriComponentsBuilder;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ajieee
 * @date 2025-10-12
 */
@Order(EtlOrder.INFO_ORDER)
@Component
@RequiredArgsConstructor
public class SseStockInfoEtl extends AbstractEtl {

	private final StockInfoMapper stockInfoMapper;

	private final RestClient restClient;

	@Override
	public void doExecute() {
		String uriString = UriComponentsBuilder.fromUriString("https://query.sse.com.cn/sseQuery/commonQuery.do")
				.queryParam("sqlId", "COMMON_SSE_CP_GPJCTPZ_GPLB_GP_L")
				.queryParam("COMPANY_STATUS", "2,4,5,7,8")
				.queryParam("type", "inParams")
				.queryParam("isPagination", false)
				.queryParam("pageHelp.pageSize", -1)
				.queryParam("pageHelp.pageNo", 1)
				.build().encode().toUriString();
		JsonNode root = restClient.get()
				.uri(uriString)
				.header("Referer", "https://www.sse.com.cn/")
				.retrieve()
				.body(JsonNode.class);
		Assert.notNull(root, "上证股票基本信息获取失败");

		JsonNode resultNode = root.get("result");
		List<StockInfoPO> pos = new ArrayList<>(resultNode.size());
		for (JsonNode node : resultNode) {
			StockInfoPO po = new StockInfoPO();

			po.setAStockCode(node.get("A_STOCK_CODE").asText());
			po.setStockName(node.get("SEC_NAME_CN").asText());
			po.setExchange(Exchange.SSE);
			po.setBStockCode("-".equals(node.get("B_STOCK_CODE").asText()) ? null : node.get("B_STOCK_CODE").asText());
			String listDate = node.get("LIST_DATE").asText();
			po.setListDate(DateUtils.parseDate(listDate, "yyyyMMdd"));
			po.setStockType(deduceStockType(node.get("STOCK_TYPE").asText()));
			po.setCompanyCode(node.get("COMPANY_CODE").asText());
			po.setCompanyName(node.get("COMPANY_ABBR").asText());
			po.setCompanyNameEn(node.get("COMPANY_ABBR_EN").asText());
			po.setCompanyNameFull(node.get("FULL_NAME").asText());
			po.setCompanyNameEnFull(node.get("FULL_NAME_IN_ENGLISH").asText());
			po.setAreaName(node.get("AREA_NAME_DESC").asText());
			po.setIndustry(deduceIndustry(node.get("CSRC_CODE_DESC").asText()));

			po.setStockCode("主板B股".equals(po.getStockType()) ? po.getBStockCode() : po.getAStockCode());

			pos.add(po);
		}

		if (!pos.isEmpty()) {
			stockInfoMapper.deleteByExchange(Exchange.SSE);
			stockInfoMapper.insert(pos);
		}
	}

	private String deduceIndustry(String industry) {
		return switch (industry) {
			case "金融业" -> "金融业";
			case "交通运输、仓储和邮政业" -> "运输仓储";
			case "制造业" -> "制造业";
			case "房地产业" -> "房地产";
			case "电力、热力、燃气及水生产和供应业" -> "水电煤气";
			case "采矿业" -> "采矿业";
			case "信息传输、软件和信息技术服务业" -> "信息技术";
			case "建筑业" -> "建筑业";
			case "批发和零售业" -> "批发零售";
			case "文化、体育和娱乐业" -> "文化传播";
			case "水利、环境和公共设施管理业" -> "公共环保";
			case "租赁和商务服务业" -> "商务服务";
			case "农、林、牧、渔业" -> "农林牧渔";
			case "住宿和餐饮业" -> "住宿餐饮";
			case "卫生和社会工作" -> "卫生";
			case "教育" -> "教育";
			case "综合" -> "综合";
			case "科学研究和技术服务业" -> "科研服务";
			default -> industry;
		};
	}

	private String deduceStockType(String stockType) {
		return switch (stockType) {
			case "1" -> "主板A股";
			case "2" -> "主板B股";
			case "8" -> "科创板";
			default -> "未知";
		};
	}

	@Override
	public EtlType getType() {
		return EtlType.SSE_STOCK_INFO;
	}
}
